/*
 * @Author: heinan
 * @Date: 2021-07-14 12:02:41
 * @Last Modified by: heinan
 * @Last Modified time: 2021-07-14 16:42:47
 */

import React, { Component } from "react";
import { Switch, Route, Redirect } from "react-router-dom";

class RouterMap extends Component {
  render() {
    const routes = this.props.routes;
    return (
      <Switch>
        {routes.map((router) => {
          return router.path === "/" ? (
            <Redirect
              from={router.path}
              to={router.redirect}
              key={router.name}
              exact
            ></Redirect>
          ) : (
            <Route
              key={router.name}
              path={router.path}
              component={(config) => {
                const RouterComponent = router.component;
                const routes = router.children ? router.children : [];
                return (
                  <RouterComponent
                    {...config}
                    routes={routes}
                  ></RouterComponent>
                );
              }}
            ></Route>
          );
        })}
      </Switch>
    );
  }
}

export default RouterMap;
